package com.hzzy.interceptor;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.hzzy.bean.BaseInfo;
import com.hzzy.enums.UserSessionKeyEnum;

/**
 * 拦截器
 * @author hjb
 * 2016年5月23日
 */
@Repository
public class JurisdictionInterceptor extends HandlerInterceptorAdapter {
	private static Logger LOGGER = Logger.getLogger(JurisdictionInterceptor.class);
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		
		// 不拦截的地址
		String[] noFilters = new String[] {"login.html", "index.htm", "index.jsp", "loadcompetenceList"};
		// 用户登录之后不拦截的
		String[] signNoFilters = new String[] {"homePage", "index.head", "index.menu", "index.welcome"};
		String defaultPath = request.getContextPath(); // //CheckonworkattendanceServer	
		String uri = request.getRequestURI();	// uri : //CheckonworkattendanceServer/homePage
//		String web_prefix = "CheckonworkattendanceServer";
		LOGGER.warn("拦截的路径：" + uri);
		
		if (uri.equals(defaultPath)) {
			//默认的路径地址
			LOGGER.warn("默认的访问地址：" + defaultPath);
		} else if (uri.indexOf(defaultPath) != -1) {
			boolean beFilter = true;
			for (String s : noFilters) {					
				// 判断是否是 不拦截的地址之一
				if (uri.indexOf(s) != -1) {
					beFilter = false;
					break;
				}
			}
			
			if (beFilter) {
				BaseInfo baseInfo = (BaseInfo) request.getSession().getAttribute(UserSessionKeyEnum.BASE_USER_INFO.getValue());
				if (baseInfo == null) {
					//未登录
					PrintWriter out = response.getWriter();
					StringBuilder builder = new StringBuilder();
					builder.append("<script type=\"text/javascript\" charset=\"UTF-8\">");  
                    builder.append("alert(\"请重新登录\");");  
                    builder.append("window.top.location.href=\"");  
                    builder.append(UserSessionKeyEnum.BASE_PATH.getValue());  
                    builder.append("/CheckonworkattendanceServer/login.html\";</script>");  
                    out.print(builder.toString());  
                    out.close();
                    return false;
				} else {
					// 判断当前访问的地址是否允许访问
					boolean isAllow = false;
					for (String s : signNoFilters) {
						if (uri.indexOf(s) != -1) {
							isAllow = true;
							break;
						}
					}
					if (!isAllow) {
						List<String> allowUrlList = baseInfo.getAllowAccessUri();
						if (allowUrlList != null && allowUrlList.size() > 0) {
							for (String s : allowUrlList) {
								if (uri.indexOf(s) != -1) {
									isAllow = true;
									break;
								}
							}
						}
					}
					if (!isAllow){
						PrintWriter out = response.getWriter();
						StringBuilder builder = new StringBuilder();
						builder.append("<script type=\"text/javascript\" charset=\"UTF-8\">");  
	                    builder.append("alert(\"尚未授权哟！\");");  
	                    builder.append("</script>");  
	                    out.print(builder.toString());  
	                    out.close();
	                    LOGGER.warn("被拦截了：" + uri);
						 return false;
					}
				}
			}
		}
		
		return super.preHandle(request, response, handler);
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		super.postHandle(request, response, handler, modelAndView);
	}
	
	
	
}
